define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bdl/bd/org/index' + location.search,
                    add_url: 'bdl/bd/org/add',
                    edit_url: 'bdl/bd/org/edit',
                    del_url: 'bdl/bd/org/del',
                    multi_url: 'bdl/bd/org/multi',
                    export_url: 'bdl/bd/org/export',
                    table: 'bd_org',
                }
            });

            var table       = $("#table");
            var showExport  = $(".btn-export").data('value')

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                search:false,
                fixedColumns: true,
                fixedRightNumber: 1,
                showToggle: false,
                showExport: showExport,
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status}  ,
                        {field: 'fnumber', title: '编码', operate: 'LIKE'},
                        {field: 'fname', title: '名称', operate: 'LIKE'},
                        {field: 'fk_bdl_bd_datacenter', title: '数据中心ID',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_datacenter_list")},
                        {field: 'datacenter.fname', title: '数据中心名称',operate: false},
                        {field: 'fk_bdl_mappingorg', title: '映射组织', operate: 'LIKE'},
                        {field: 'fk_bdl_comment', title: '备注', operate: 'LIKE'},
                        {field: 'fk_bdl_creatorid', title: '创建人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                        {field: 'creator.nickname', title: '创建人', operate: false},
                        {field: 'fk_bdl_createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'fk_bdl_fmodifierid', title: '修改人',visible:false,searchList: $.getJSON("bdl/bd/datacenter/getFilter?type=get_admin_list")},
                        {field: 'modifier.nickname', title: '修改人', operate: false},
                        {field: 'fk_bdl_modifytime', title: '修改时间', operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'bdl/bd/org/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'fenable', title: '使用状态', searchList: {"1":'可用',"0":'禁用'}, formatter: Table.api.formatter.status}  ,
                        {field: 'fnumber', title: '编码', operate: 'LIKE'},
                        {field: 'fname', title: '名称', operate: 'LIKE'},
                        {field: 'fk_bdl_bd_datacenter', title: '数据中心ID', operate: 'LIKE'},
                        {field: 'fk_bdl_comment', title: '备注', operate: 'LIKE'},
                        {
                            field: 'fk_bdl_deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'bdl/bd/org/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'bdl/bd/org/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function ()
        {
            type_change();
            Controller.api.bindevent();
        },
        edit: function ()
        {
            type_change();
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    //js控制type对应的资源显示与隐藏
    function type_change()
    {
        $(document).ready(function(){
            $(document).on('change','#c-type',function(){
                var that = $(this)
                var val  = that.val()

                if(val == 3)
                {
                    $(".banner-add-url").css('display','none')
                    $(".banner-add-object_id").css('display','none')
                }else if(val == 1){
                    $(".banner-add-url").css('display','block')
                    $(".banner-add-object_id").css('display','none')
                }else if(val == 2){
                    $(".banner-add-url").css('display','none')
                    $(".banner-add-object_id").css('display','block')
                }

            })
        })
    }
    return Controller;
});
